package xfsale.service.impl.j2ee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.log4j.Logger;

import xfsale.domain.SysMenuItem;
import xfsale.domain.SysDictItem;
import xfsale.domain.SysDictValue;

public final class PubSys {

	private String workDay;			//工作日期
	private String workTime;		//工作时间
	private String sysDate;			//系统日期
	private String sysTime;			//系统时间
	private String status;			//系统状态
	private String version;			//系统版本
	private String distributorCode;	//销售商代码
	private String nextWorkDay;		//下一交易日
	private int logLevel;			//日志级别
	private int logTo;				//日志目标
	private String logFile;			//日志文件

	private Map<String, SysDictItem> dictItems = new HashMap<String, SysDictItem>();
	private List<SysMenuItem> menuItems = new ArrayList<SysMenuItem>();

	private Connection conn;		//数据库连接
	private Logger logger = Logger.getLogger(PubSys.class);	//日志

	public PubSys(Connection conn) {
		this.conn = conn;
	}

	public String getWorkDay() {
		return workDay;
	}

	public String getWorkTime() {
		return workTime;
	}

	public String getSysDate() {
		return sysDate;
	}

	public String getSysTime() {
		return sysTime;
	}

	public String getStatus() {
		return status;
	}

	public String getVersion() {
		return version;
	}

	public String getDistributorCode() {
		return distributorCode;
	}

	public String getNextWorkDay() {
		return nextWorkDay;
	}

	public int getLogLevel() {
		return logLevel;
	}

	public int getLogTo() {
		return logTo;
	}

	public String getLogFile() {
		return logFile;
	}

	public Map<String, SysDictItem> getDictItems() {
		return dictItems;
	}

	public List<SysMenuItem> getMenuItems() {
		return menuItems;
	}

	public void initParam() throws Exception {
		logger.info("初始化系统参数...");

		PreparedStatement stmt = conn.prepareStatement("" +
			"select paramValue " +
			"from SysParam " +
			"where upper(paramKey) = upper(?)"
		);
		ResultSet rs = null;

		try {
			//获取系统状态
			logger.debug("获取系统状态...");
			stmt.setString(1, "SysStatus");
			rs = stmt.executeQuery();
			rs.next();
			status = rs.getString(1);
			logger.debug("系统状态: " + status);

			//获取系统版本
			logger.debug("获取系统版本...");
			stmt.setString(1, "SysVersion");
			rs = stmt.executeQuery();
			rs.next();
			version = rs.getString(1);
			logger.debug("系统版本:" + version);

			//获取系统工作日
			logger.debug("获取系统工作日...");
			stmt.setString(1, "WorkDay");
			rs = stmt.executeQuery();
			rs.next();
			workDay = rs.getString(1);
			logger.debug("系统工作日: " + workDay);

			//获取系统下一工作日
			logger.debug("获取系统下一工作日...");
			stmt.setString(1, "NextWorkDay");
			rs = stmt.executeQuery();
			rs.next();
			nextWorkDay = rs.getString(1);
			logger.debug("系统下一工作日: " + nextWorkDay);

			//获取销售商代码
			logger.debug("获取销售商代码...");
			stmt.setString(1, "DistributorCode");
			rs = stmt.executeQuery();
			rs.next();
			distributorCode = rs.getString(1);
			logger.debug("销售商代码: " + distributorCode);

			//获取日志级别
			logger.debug("获取日志级别...");
			stmt.setString(1, "LogLevel");
			rs = stmt.executeQuery();
			rs.next();
			logLevel = rs.getInt(1);
			logger.debug("日志级别: " + logLevel);

			//获取日志目标
			logger.debug("获取日志目标...");
			stmt.setString(1, "LogTo");
			rs = stmt.executeQuery();
			rs.next();
			logTo = rs.getInt(1);
			logger.debug("日志目标: " + logTo);

			//获取日志文件
			logger.debug("获取日志文件...");
			stmt.setString(1, "LogFile");
			rs = stmt.executeQuery();
			rs.next();
			logFile = rs.getString(1);
			logger.debug("日志文件:" + logFile);

		} catch (Exception e) {
			logger.error("初始化系统参数出错. " + e);
			throw e;
		} finally {
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
		}

		//获取系统日期和时间
		Date now = new Date();
		SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat timeFmt = new SimpleDateFormat("HHmmss");
		sysDate = dateFmt.format(now);
		sysTime = timeFmt.format(now);

		//获取交易时间
		workTime = sysTime;
	}

	public void initMenu() throws Exception {
		logger.info("初始化系统菜单...");

		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			logger.debug("获取系统菜单...");
			stmt = conn.prepareStatement("" +
				"select * from SysMenu " +
				"where menuEnabled = '1' " +
				"order by menuId asc "
			);
			rs = stmt.executeQuery();
			while (rs.next()) {
				SysMenuItem mi = new SysMenuItem();
				mi.menuId = rs.getString("menuId");
				mi.menuCaption = rs.getString("menuCaption");
				mi.menuIcon = rs.getString("menuIcon");
				mi.menuOrder = 0;
				mi.serviceCode = rs.getString("serviceCode");
				mi.uiDll = rs.getString("uiDll");
				mi.uiFrame = rs.getString("uiFrame");
				mi.uiIdentify = rs.getString("uiIdentify");
				mi.callService = rs.getString("callService");
				mi.callMethod = rs.getString("callMethod");
                mi.callProc = rs.getString("callProc");
                mi.summary = rs.getString("summary");
				menuItems.add(mi);
			}
			logger.debug("初始化系统菜单成功.");
		} catch (Exception e) {
			logger.error("初始化系统菜单出错. " + e);
			throw e;
		} finally {
			stmt.close();
		}
	}

	public void initDict() throws Exception {
		logger.info("初始化系统字典...");

		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.prepareStatement("" +
				"select * from XmDict " +
				"order by itemName"
			);
			rs = stmt.executeQuery();
			while (rs.next()) {
				SysDictItem di = new SysDictItem();
				di.itemName = rs.getString("itemName");
				di.itemType = rs.getString("itemType");
				di.itemScale = rs.getInt("itemScale");
				di.itemPrecise = rs.getInt("itemPrecise");
				di.itemDesc = rs.getString("itemDesc");
				di.itemVerify = rs.getString("itemVerify");
				di.itemDefault = rs.getString("itemDefault");
				dictItems.put(di.itemName.toUpperCase(), di);
			}
			stmt.close();

			stmt = conn.prepareStatement("" +
				"select * from XmDictValue " +
				"order by itemName, itemValue"
			);
			rs = stmt.executeQuery();
			while (rs.next()) {
				String itemName = rs.getString("itemName");
				if (!dictItems.containsKey(itemName.toUpperCase()))
					continue;

				SysDictItem di = dictItems.get(itemName.toUpperCase());
				SysDictValue dv = new SysDictValue();
				dv.itemValue = rs.getString("itemValue");
				dv.valueDesc = rs.getString("valueDesc");
				di.valueItems.put(dv.itemValue, dv);
			}
			logger.debug("初始化系统字典成功.");
		} catch (Exception e) {
			logger.error("初始化系统字典出错. " + e);
			throw e;
		} finally {
			stmt.close();
		}
	}

}
